/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.c;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnCollection;
import com.ykht.proj.coupon.entity.CnPerson;
import com.ykht.proj.coupon.service.CnCollectionService;
import com.ykht.proj.coupon.vo.CnCollectionPageReq;
import com.ykht.proj.coupon.vo.CnCollectionPageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 收藏表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@RestController
@AllArgsConstructor
@RequestMapping("c/coll")
@Api(value = "cncollection", tags = "c端收藏相关")
@Slf4j
public class CollectionController {

	private final CnCollectionService cnCollectionService;

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @param req  收藏表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R<IPage<CnCollectionPageResp>> getCnCollectionPage(Page page, CnCollectionPageReq req) {
		req.setPerId(SecurityUtils.getPersonId());
		IPage<CnCollectionPageResp> resp = cnCollectionService.opPage(page, req);
		return R.ok(resp);
	}

	@ApiOperation(value = "我的收藏店铺外显图", notes = "我的收藏店铺外显图")
	@GetMapping("/picList")
	public R<List<String>> getCnColPicList(Page page, CnCollectionPageReq req) {
		return R.ok(cnCollectionService.opList(page));
	}

	@ApiOperation(value = "添加收藏", notes = "添加收藏")
	@PostMapping("add")
	public R add(@RequestBody CnCollection cnCollection) {

		log.info("->添加收藏：{}", JSONUtil.toJsonStr(cnCollection));

		Long loginId = SecurityUtils.getPersonId();
		CnPerson cnPerson = new CnPerson();
		cnPerson.setId(loginId);
		cnPerson = cnPerson.selectById();
		//有手机号
		if (0 != cnPerson.getAuthBaseStatus()) return R.failed("未授权微信基础信息");

		CnCollection temp = cnCollectionService.getOne(Wrappers.lambdaQuery(CnCollection.class)
				.eq(CnCollection::getPerId, loginId).eq(CnCollection::getShopId, cnCollection.getShopId()));
		if (temp != null) return R.ok();

		cnCollection.setPerId(loginId);
		cnCollection.setCollectTime(LocalDateTime.now());
		cnCollection.setCreateTime(LocalDateTime.now());
		cnCollection.setCreateUser(loginId);
		return R.ok(cnCollectionService.save(cnCollection));
	}

	@ApiOperation(value = "移除收藏", notes = "移除收藏")
	@PostMapping("/remove")
	public R removeById(@RequestBody CnCollection cnCollection) {
		log.info("->移除收藏：{}", JSONUtil.toJsonStr(cnCollection));
		Long loginId = SecurityUtils.getPersonId();
		CnPerson cnPerson = new CnPerson();
		cnPerson.setId(loginId);
		cnPerson = cnPerson.selectById();
		//有手机号
		if (0 != cnPerson.getAuthBaseStatus()) return R.failed("未授权微信基础信息");
		cnCollectionService.remove(Wrappers.lambdaQuery(CnCollection.class)
				.eq(CnCollection::getShopId, cnCollection.getShopId()).eq(CnCollection::getPerId, SecurityUtils.getPersonId()));
		return R.ok();
	}

}
